package com.xxr.mom.ci.common.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @description: ReflectionUtil
 * @date: 2024/8/11 16:48
 * @author: natsuki_kining
 * @version: 1.0
 */
public class ReflectionUtil {

    /**
     * 获取泛型类型
     * @param instance 对象
     * @param parameterIndex 泛型参数index
     * @return
     */
    public static Class<?> findSuperClassParameterType(Object instance, int parameterIndex) {
        Class<?> subClass = instance.getClass();
        ParameterizedType pt = (ParameterizedType) (subClass.getGenericSuperclass());
        Type actualTypeArgument = pt.getActualTypeArguments()[parameterIndex];
        if (actualTypeArgument instanceof ParameterizedType) {
            return (Class<?>)((ParameterizedType)actualTypeArgument).getActualTypeArguments()[0];
        }
        return (Class<?>) actualTypeArgument;
    }

}
